package com.blb.edusearchservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.blb.common.entity.Course;
import com.blb.common.entity.PageEntity;
import com.blb.common.util.ResponseResult;
import com.blb.edusearchservice.dao.ElasticsearchDAO;
import com.blb.edusearchservice.entity.ElasticsearchEntity;
import com.blb.edusearchservice.feign.CourseFeignClient;
import com.blb.edusearchservice.service.CourseIndexService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程索引业务实现类
 */
@Service
public class CourseIndexServiceImpl implements CourseIndexService {

    public static final String INDEX_NAME = "course";

    @Autowired
    private CourseFeignClient courseFeignClient;

    @Autowired
    private ElasticsearchDAO elasticsearchDAO;

    @Override
    public void initCourseIndex() throws IOException {
        //判断是否存在课程索引
        boolean exists = elasticsearchDAO.existIndex(INDEX_NAME);
        if(exists){
            //如果存在就删除索引
            elasticsearchDAO.deleteIndex(INDEX_NAME);
        }
        //如果不存在就创建索引
        elasticsearchDAO.createIndex(INDEX_NAME);
        //调用课程微服务获得所有课程
        ResponseResult<List<Course>> courses = courseFeignClient.getAllCourses();
        List<ElasticsearchEntity> entities = new ArrayList<>();
        courses.getData().forEach(course -> {
            entities.add(new ElasticsearchEntity(String.valueOf(course.getId()),course));
        });
        //批量插入ES
        elasticsearchDAO.insertBatch(INDEX_NAME,entities);
    }

    /**
     * 搜索课程
     * @param args
     * @return
     */
    @Override
    public PageEntity<Course> searchCourses(Map<String, String> args) throws IOException {
        int from = 0;
        if(args.containsKey("current")){
            from = Integer.parseInt(args.get("current"));
        }
        int size = 10;
        if(args.containsKey("size")){
            size = Integer.parseInt(args.get("size"));
        }
        Map<String,String> search = new HashMap<>();
        if(args.containsKey("search")){
            search = JSON.parseObject(args.get("search"), Map.class);
        }
        Map<String,String> sort = new HashMap<>();
        if(args.containsKey("sort")){
            sort = JSON.parseObject(args.get("sort"), Map.class);
        }
        PageEntity<Course> entity = elasticsearchDAO.searchPageByMap(INDEX_NAME, from, size, search, sort, Course.class,
                "courseName","brief");
        return entity;
    }

    @Override
    public void saveCourse(Course course) throws IOException {
        elasticsearchDAO.saveOrUpdate(INDEX_NAME,new ElasticsearchEntity(String.valueOf(course.getId()),course));
    }

    @Override
    public void removeCourse(String id) throws IOException {
        elasticsearchDAO.deleteById(INDEX_NAME,id);
    }

}
